


//
//  UTableVC.m
//  BusinessMarket
//
//  Created by Michael Li on 2018/11/20.
//  Copyright © 2018年 Michael Li. All rights reserved.
//

#import "DemandIntroductVC.h"
#import "DemandTheHallContentHeadView.h"
#import "DemandTheHallContentHeadCell.h"
#import "HomeServiceDetailsIntroductionContentTwoCell.h"
#import "WriteRequirementCollectionCell.h"
#import "HomeModel.h"
#import "WriteRequirementTitleCell.h"
#import "DemandCell.h"
#import "ButtonCell.h"
#import "TextFieldCell.h"
#import "CellFrameModel.h"
@interface DemandIntroductVC ()<ButtonCellDelegate,DemandCellDelegate,UIScrollViewDelegate>
@property(nonatomic,strong) DemandTheHallContentHeadView *headView;
@property(nonatomic,strong) NSString *pageNumber;
@property(nonatomic,strong) NSString *pageSize;
@property(nonatomic,strong) NSString *IndustryIds;
@property(nonatomic,strong) NSMutableArray *demandArray;
@property(nonatomic,strong) NSDictionary *dict;
@property(nonatomic,strong) AFHTTPSessionManager *manager;
@property(nonatomic,strong) NSString *stringUrl;
@property(nonatomic,strong) UIView *sectionHeadView;

@end
static NSString *identifierWriteRequirementCollectionCell = @"WriteRequirementCollectionCell";
static NSString *identifierWriteRequirementTitleCell = @"WriteRequirementTitleCell";
static NSString *identifierDemandCell = @"DemandCell";
static NSString *identifierButtonCell = @"ButtonCell";
static NSString *identifierHomeServiceDetailsIntroductionContentTwoCell = @"HomeServiceDetailsIntroductionContentTwoCell";
static NSString *identifierCell = @"Cell";
@implementation DemandIntroductVC
-(NSMutableArray *)demandArray
{
    if (!_demandArray) {
        _demandArray = [NSMutableArray array];
        
    }
    return _demandArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"需求详情";
    self.view.backgroundColor = [UIColor whiteColor];
    self.headView = [[DemandTheHallContentHeadView alloc]initWithFrame:CGRectMake(0, 0, kVWidth, 180)];
    self.headView.model = self.homeModel;
    self.tableView.tableHeaderView = self.headView;
    self.manager = [AFHTTPSessionManager manager];
    //    self.tableView.backgroundColor = kOrangeBg;
     XLog(@"需求概括:%@",self.homeModel.represent);
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"WriteRequirementTitleCell" bundle:nil] forCellReuseIdentifier:identifierWriteRequirementTitleCell];
    [self.tableView registerClass:[WriteRequirementCollectionCell class] forCellReuseIdentifier:identifierWriteRequirementCollectionCell];
    [self.tableView registerClass:[HomeServiceDetailsIntroductionContentTwoCell class] forCellReuseIdentifier:identifierHomeServiceDetailsIntroductionContentTwoCell];
     [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:identifierCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"ButtonCell" bundle:nil] forCellReuseIdentifier:identifierButtonCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"DemandCell" bundle:nil] forCellReuseIdentifier:identifierDemandCell];
    self.pageNumber =@"1";
    self.pageSize =@"10";
    self.IndustryIds = @"1";
    [self relaodInternetData];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(senderDemandDict:) name:@"senderDemandDict" object:nil];
    
    self.sectionHeadView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kVWidth, 40)];
    [self.view addSubview:self.sectionHeadView];
    self.sectionHeadView.backgroundColor = kRedColor;
    for (int i = 0; i < 2; i++) {
        UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(kVWidth/2*i, 0, kVWidth/2, 40)];
        [self.sectionHeadView addSubview:btn];
        NSArray *titles = @[@"详情介绍",@"推荐"];
        [btn setTitle:titles[i] forState:UIControlStateNormal];
        if (i == 0) {
            [btn setTitleColor:kOrangeBg forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont systemFontOfSize:16];
        }else {
            [btn setTitleColor:kLabel153Color forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont systemFontOfSize:14];
        }
    }
//    self.sectionHeadView.hidden = YES;
}
-(void)senderDemandDict:(NSNotification *)info
{
    NSDictionary *dict = info.userInfo;
    XLog(@"dict:%@",dict);
  
}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
-(void)relaodInternetData
{
    [WSProgressHUD setProgressHUDIndicatorStyle:WSProgressHUDIndicatorBigGray];
    [WSProgressHUD show];
    self.dict = @{
                  @"pageNum":self.pageNumber,
                  @"pageSize":self.pageSize,
                  @"IndustryIds":self.IndustryIds
                  };
    XLog(@"self.dict:%@",self.dict);
    self.stringUrl = [NSString stringWithFormat:@"%@/demand/selectDemandByIndustryIds",kUrl];
    [self.manager POST:self.stringUrl parameters:self.dict progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *temp = [responseObject objectForKey:@"date"];
        XLog(@"temp:%@",temp);
        NSDictionary *data = [temp objectForKey:@"data"];
        XLog(@"data:%@",data);
        for (NSDictionary *dict in data) {
            HomeModel *model =[HomeModel mj_objectWithKeyValues:dict];
            [self.demandArray addObject:model];
        }
        XLog(@"self.demandArray:%ld",self.demandArray.count);
        [self.tableView reloadData];
        [WSProgressHUD dismiss];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        XLog(@"error:%@",error);
    }];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    XLog(@"scrollView.contentOffset.y:%f",scrollView.contentOffset.y);
    if (scrollView.contentOffset.y>=200) {
        self.sectionHeadView.hidden = NO;
    }else {
        self.sectionHeadView.hidden = YES;
    }
    if (self.returnValueBlock) {
        self.returnValueBlock(scrollView.contentOffset.y);
    };
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if(section == 0){
        return 10;
    }else {
        return self.demandArray.count?self.demandArray.count:0;
        //        return 3;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView  setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    if (indexPath.section == 0) {
        if (indexPath.row== 0||indexPath.row== 6||indexPath.row== 9) {
            WriteRequirementTitleCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierWriteRequirementTitleCell forIndexPath:indexPath];
            if (indexPath.row == 0) {
                cell.titleL.text = @"需求细节信息";
            }else if (indexPath.row == 6) {
                cell.titleL.text = @"需求详细描述";
            }else{
                cell.titleL.text = @"相似需求推荐";
            }
            
            return cell;
        }else  if (indexPath.row== 7) {
           UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierCell forIndexPath:indexPath];
            cell.textLabel.numberOfLines = 0;
            cell.textLabel.font = [UIFont systemFontOfSize:13];
            cell.textLabel.textColor = kLabel102Color;
            cell.textLabel.text = self.homeModel.represent;
            return cell;
        }else  if (indexPath.row== 8) {
            ButtonCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierButtonCell forIndexPath:indexPath];
            [cell.btn setTitle:@"立即联系" forState:UIControlStateNormal];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.delegate = self;
            return cell;
        }else {
            HomeServiceDetailsIntroductionContentTwoCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierHomeServiceDetailsIntroductionContentTwoCell forIndexPath:indexPath];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.indexPath = indexPath;
//            cell.sendDemandHomeModel = self.homeModel;
            CellFrameModel *frameModel = [[CellFrameModel alloc]init];
            frameModel.homeModel = self.homeModel;
            cell.frameModel = frameModel;
            return cell;
            
        }
    }else {
        DemandCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierDemandCell forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.delegate = self;
        cell.indexPath = indexPath;
        HomeModel *model = self.demandArray[indexPath.row];
        cell.homeModel = model;
//        cell.homeModel.showView = !self.homeModel.showView;
        return cell;
    }
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
     if (indexPath.section == 0){
        if (indexPath.row== 0||indexPath.row== 6){
            return 60;
        }else {
            
            CellFrameModel *frameMode = [[CellFrameModel alloc]init];
            frameMode.homeModel = self.homeModel;
            XLog(@"cellHeight:%ld",frameMode.cellHeight);
//            cell.sendDemandHomeModel = self.homeModel;
            CellFrameModel *frameModel = [[CellFrameModel alloc]init];
            frameModel.homeModel = self.homeModel;
//            cell.frameModel = frameModel;
            XLog(@"frameModel.cellHeight:%ld",(long)frameModel.cellHeight);
            return frameModel.cellHeight+20;

        }
    }else {
        return 144;
    }
}
#pragma mark-代理:最新需求
-(void)DemandCell:(DemandCell *)DemandCell withClickBtn:(UIButton *)clickBtn withIndexPath:(nonnull NSIndexPath *)indexPath withHomeModel:(nonnull HomeModel *)homeModel
{
    HomeModel *model = self.demandArray[indexPath.row];
    model.show = clickBtn.selected;
    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    
}
#pragma mark-代理---立即联系
- (void)buttonCell:(ButtonCell *)buttonCell withClickBtn:(UIButton *)clickBtn
{
    
    
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 40)];
    headerView.backgroundColor = [UIColor whiteColor];
    if (section == 0) {
        for (int i = 0; i < 2; i++) {
            UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(kVWidth/2*i, 0, kVWidth/2, 40)];
            [headerView addSubview:btn];
            NSArray *titles = @[@"详情介绍",@"推荐"];
            [btn setTitle:titles[i] forState:UIControlStateNormal];
            if (i == 0) {
                [btn setTitleColor:kOrangeBg forState:UIControlStateNormal];
                btn.titleLabel.font = [UIFont systemFontOfSize:16];
            }else {
                [btn setTitleColor:kLabel153Color forState:UIControlStateNormal];
                 btn.titleLabel.font = [UIFont systemFontOfSize:14];
            }
        }
        return headerView;
    }else {
        return nil;
    }
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 40;
    }else {
        return 0.1;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 0,1)];
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1;
}
@end
